package com.friend.campusnotice;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.friend.campusnotice.adapter.ContactListAdapter;
import com.friend.campusnotice.adapter.MyPagerAdapter;
import com.friend.campusnotice.bean.CallLog;
import com.friend.campusnotice.bean.Contacts;
import com.friend.campusnotice.util.MyUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;





public class MainActivity extends FragmentActivity {

    private RecyclerView recyleView;
    private ContactListAdapter adapter;
    private List<Contacts> contactsList;
    private Map<String, Object> myData;
    private List<CallLog> callLogList;


    ///
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private LayoutInflater mInflater;

    private List<String> mTitleList = new ArrayList<>();//页卡标题集合
    //private View view1, view2;//页卡视图
    private List<Fragment> mViewList = new ArrayList<>();//页卡视图集合




    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter.notifyDataSetChanged();
          // Toast.makeText(MainActivity.this,"加载完了",Toast.LENGTH_LONG).show();

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        initData();
       // initView();

        initTabView();

    }

    private void initTabView() {

        mViewPager = (ViewPager) findViewById(R.id.vp_view);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);

        mInflater = LayoutInflater.from(this);

//        view1 = mInflater.inflate(R.layout.contacts_layout, null);
//        view1  =mInflater.inflate(R.layout.call_log_layout, null);

        ContactsFragment contactFragment = new ContactsFragment();
        CallLogFragment callLogFragment = new CallLogFragment();


        //添加页卡视图
        mViewList.add(contactFragment);
        mViewList.add(callLogFragment);

        //添加页卡标题
        mTitleList.add("联系人");
        mTitleList.add("通话记录");



        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式，当前为系统默认模式

        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
               // mTabLayout.addTab(mTabLayout.newTab().setText("联系人"));
               // mTabLayout.addTab(mTabLayout.newTab().setText("通话记录"));
              //  tabLayout.addTab(tabLayout.newTab().setText("全部"));
               // tabLayout.setOnTabSelectedListener((TabLayout.OnTabSelectedListener) activity);
            }
        });


        mTabLayout.addTab(mTabLayout.newTab().setText("联系人"));//添加tab选项卡
        mTabLayout.addTab(mTabLayout.newTab().setText("通话记录"));
        // 设置tab文本的没有选中（第一个参数）和选中（第二个参数）的颜色
        mTabLayout.setTabTextColors(getResources().getColor(R.color.colorAccent), Color.WHITE);

        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager(),mViewList);
        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            Toast.makeText(this," activity 获取了返回值",Toast.LENGTH_LONG).show();
        }
    }

    //
//    private void initData() {
//
//        new Thread(){
//            @Override
//            public void run() {
//                contactsList.clear();
//                MyUtil myUtil = new MyUtil();
//                myData = myUtil.getPhoneContacts(MainActivity.this);
//                callLogList = myUtil.GetCall(MainActivity.this);
////                for(int i =0;i<callLogList.size();i++){
////                    if(callLogList.get(i).getName() !=null ){
////                        Log.i("xiaohei",callLogList.get(i).getName() +" "+ callLogList.get(i).getCallType());
////                    }else {
////                        Log.i("xiaohei",callLogList.get(i).getPhoneNum()+" "+ callLogList.get(i).getCallType());
////                    }
////
////                }
//             //   Log.i("xiaohei","获取的长度 "+callLogList.size() );
//                ArrayList<String> name = (ArrayList<String>) myData.get("ContactsName");
//                ArrayList<String> number = (ArrayList<String>) myData.get("ContactsNumber");
//                ArrayList<Long> ContactId = (ArrayList<Long>) myData.get("ContactId");
//                ArrayList<Long> ContactIconId = (ArrayList<Long>) myData.get("ContactsIconId");
//                for (int i = 0; i < name.size(); i++) {
//                    Contacts contact = new Contacts();
//                    contact.setContactName(name.get(i));
//                    contact.setPhoneNumber(number.get(i));
//                    contact.setContact_Id(ContactId.get(i));
//                    contact.setPhotoid(ContactIconId.get(i));
//                    //contact.setUserHead(phonto.get(i));
//                    contactsList.add(contact);
//                    handler.sendEmptyMessage(0);
//                }
//            }
//        }.start();
//
//
//
//
//    }

//    private void initView() {
//        recyleView = (RecyclerView) findViewById(R.id.swipe_target);
//        contactsList = new ArrayList<Contacts>();
//        adapter = new ContactListAdapter(this, contactsList);
//        adapter.setOnItemClickListener(new ContactListAdapter.onItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                Toast.makeText(MainActivity.this,"获取的位置"+ position,Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(MainActivity.this,ContactDetailActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putParcelable("contact", contactsList.get(position));
//                intent.putExtras(bundle);
//                startActivity(intent);
//
//            }
//        });
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//        recyleView.setLayoutManager(layoutManager);
//        recyleView.setAdapter(adapter);
//
//    }
}
